# -*- coding:utf-8 -*-
# created on 2017/3/6
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from sympy import Mul, cos, pi


class XLTriangleGetExpression(BaseFunction):
    def solver(self, *args):
        express = args[0].sympify()
        v_triangle = self.search('vTriangle')
        v_triangle.condition_transform()
        if isinstance(express, Mul) and len(express.args) == 2:  # \\overrightarrow{BC}•\\overrightarrow{CA}
            vector1 = express.args[0]
            vector2 = express.args[1]
            vector1_name = str(vector1.args[0])
            vector2_name = str(vector2.args[0])
            if (v_triangle.line_To_A_name == vector1_name) or (
                    v_triangle.line_To_A_name == (vector1_name[1] + vector1_name[0])):
                vector1_value = v_triangle.line_To_A_value
            elif (v_triangle.line_To_A_name == vector2_name) or (
                    v_triangle.line_To_A_name == (vector2_name[1] + vector2_name[0])):
                vector2_value = v_triangle.line_To_A_value

            if (v_triangle.line_To_B_name == vector1_name) or (
                    v_triangle.line_To_B_name == (vector1_name[1] + vector1_name[0])):
                vector1_value = v_triangle.line_To_B_value
            elif (v_triangle.line_To_B_name == vector2_name) or (
                    v_triangle.line_To_B_name == (vector2_name[1] + vector2_name[0])):
                vector2_value = v_triangle.line_To_B_value

            if (v_triangle.line_To_C_name == vector1_name) or (
                    v_triangle.line_To_C_name == (vector1_name[1] + vector1_name[0])):
                vector1_value = v_triangle.line_To_C_value
            elif (v_triangle.line_To_C_name == vector2_name) or (
                    v_triangle.line_To_C_name == (vector2_name[1] + vector2_name[0])):
                vector2_value = v_triangle.line_To_C_value

            vector1_left_name = vector1_name[0]
            vector1_right_name = vector1_name[1]
            vector2_left_name = vector2_name[0]
            if vector1_left_name == vector2_left_name:
                vector1_vector2_jiajiao = vector1_left_name
                if vector1_vector2_jiajiao == v_triangle.jiao_A_name:
                    if str(v_triangle.jiao_A_value.func) == str("d"):
                        vector1_vector2_jiajiao_value = v_triangle.jiao_A_value.args[0]
                elif vector1_vector2_jiajiao == v_triangle.jiao_B_name:
                    if str(v_triangle.jiao_B_value.func) == str("d"):
                        vector1_vector2_jiajiao_value = v_triangle.jiao_B_value.args[0]
                elif vector1_vector2_jiajiao == v_triangle.jiao_C_name:
                    if str(v_triangle.jiao_C_value.func) == str("d"):
                        vector1_vector2_jiajiao_value = v_triangle.jiao_C_value.args[0]
            else:
                if vector1_right_name == vector2_left_name:
                    vector1_vector2_jiajiao = vector1_right_name
                    if vector1_vector2_jiajiao == v_triangle.jiao_A_name:
                        if str(v_triangle.jiao_A_value.func) == str("d"):
                            vector1_vector2_jiajiao_value = 180 - v_triangle.jiao_A_value.args[0]
                    elif vector1_vector2_jiajiao == v_triangle.jiao_B_name:
                        if str(v_triangle.jiao_B_value.func) == str("d"):
                            vector1_vector2_jiajiao_value = 180 - v_triangle.jiao_B_value.args[0]
                    elif vector1_vector2_jiajiao == v_triangle.jiao_C_name:
                        if str(v_triangle.jiao_C_value.func) == str("d"):
                            vector1_vector2_jiajiao_value = 180 - v_triangle.jiao_C_value.args[0]
                else:
                    pass
            self.steps.append(["", "依题意，得"])
            express_value = vector1_value * vector2_value * cos(vector1_vector2_jiajiao_value / 180 * pi)
            express_value = express_value.expand().simplify()
            self.steps.append(["", "%s = %s" % (new_latex(express), new_latex(express_value))])
            self.output.append(BaseNumber(express_value))
        return self
